package com.yidu.InventoryManagement.service.impl;

import com.yidu.BusinessData.entity.Security;
import com.yidu.InventoryManagement.entity.Fund;
import com.yidu.InventoryManagement.entity.SecurityAccrualStock;
import com.yidu.InventoryManagement.mapper.SecurityAccrualStockMapper;
import com.yidu.InventoryManagement.service.securityAccrualStockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author z
 * @version 1.0
 * @date 2022/12/6
 */
@Service
public  class securityAccrualStockServiceImpl implements securityAccrualStockService {
    @Resource
    private SecurityAccrualStockMapper securityAccrualStockMapper;
    /**
     * 证券库存查询方法 分页
     * @return
     */
    public Map selectSecuritiesInventory(int limit, int page,String date,String securityType) {
        String strWhere=append(date,securityType);

        Map layUiMap=new HashMap();

        Map map=new HashMap();
      //分页
        map.put("limit",limit);
        map.put("page",(page-1)*limit);
        map.put("strWhere",strWhere);


        layUiMap.put("msg","");
        layUiMap.put("code",0);
        layUiMap.put("data",securityAccrualStockMapper.selectSecuritiesInventory(map));
        layUiMap.put("count",securityAccrualStockMapper.selectSecuritiesInventoryInfo(map));

        return layUiMap;
    }

    public String append(String date, String securityType){
        // //搜索日期跟类型的字符串拼接
        StringBuffer term=new StringBuffer();
        if(date!=null && !date.equals("")){
            term.append(" and date=").append(" '"+date+" '");
        }
        if(securityType!=null && !securityType.equals("")){
            term.append(" and securityType=").append(securityType);
        }

        return term.toString();
    }

    public int addSecuritiesInventoryInfo(SecurityAccrualStock securityAccrualStock) {
        return securityAccrualStockMapper.addSecuritiesInventoryInfo(securityAccrualStock);

    }

    public String getmaxsecurityAccrualStockId() {
        return securityAccrualStockMapper.getmaxsecurityAccrualStockId();
    }

    //修改
    public int updateSecuritiesInventoryInfo(SecurityAccrualStock securityAccrualStock) {
     /* securityAccrualStockMapper.updateSecuritiesInventoryInfo(securityAccrualStock);
        return 0;*/
        return securityAccrualStockMapper.updateSecuritiesInventoryInfo(securityAccrualStock);
    }
    /**
     * 删除（单个删除和多个删除）
     *
     * @param
     * @return list 集合
     */
    public int deleteSecuritiesInventoryInfo(String securityAccrualStockId) {
        System.out.println(securityAccrualStockId+"pppppp");
        //将id转为数据
        String[] split = securityAccrualStockId.split(",");
        ArrayList<Object> list = new ArrayList<Object>();
        for (String id : split) {
            //将id存入集合
            list.add(id);
        }
        int i = securityAccrualStockMapper.deleteSecuritiesInventoryInfo(list);
        return i;
    }


    @Override
    public int selectSecurityAccrualStockId() {
        return securityAccrualStockMapper.selectSecurityAccrualStockId();
    }

    /**
     * 下拉表格的查询基金信息
     * @param page 当前页
     * @param limit 每页显示数
     * @param fundName 基金名称
     * @return list 集合
     */

    public Map selectFundIdAndFundName(int page, int limit, String fundName) {
        Map map = new HashMap();
        map.put("page", (page-1)*limit);
        map.put("limit", limit);
        map.put("term", append2(fundName));
        List<Fund> securities = securityAccrualStockMapper.selectFundIdAndFundName(map);

        //查行数
        int count=securityAccrualStockMapper.selectFundIdAndFundNameCount(map);
        Map layUiMap = new HashMap();
        layUiMap.put("code", 0);
        layUiMap.put("msg", "");
        layUiMap.put("count", count);
        layUiMap.put("data", securities);
        return layUiMap;
    }

    /**
     * 下拉框基金信息，拼接条件的方法
     * @param fundName 基金名称
     * @return
     */
    private String append2(String fundName) {
        String sql = "";
        if (fundName != null && !fundName.equals("")) {
            sql += " and fundName=" + fundName;
        }
        return sql;
    }
    /**
     * 分页查询下拉表格的查询基金信息
     * @param fundName 基金名称
     * @return
     */
    public int selectFundIdAndFundNameCount(String fundName) {
        Map map = new HashMap();
        map.put("fundName", fundName);
        int count = securityAccrualStockMapper.selectFundIdAndFundNameCount(map);
        return count;
    }



    /**
     * 下拉表格的查询证券信
     * @param page         当前页
     * @param limit        每页显示数
     * @param securityName 证券名称
     * @return list 集合
     */
    public Map selectSecurityIdAndSelectSecurityName(int page, int limit, String securityName) {
        Map map = new HashMap();
        map.put("page", (page-1)*limit);
        map.put("limit", limit);
        map.put("term", append(securityName));
        List<Security> securities = securityAccrualStockMapper.selectSecurityIdAndSelectSecurityName(map);

        //查行数
        int count=securityAccrualStockMapper.selectSecurityIdAndSelectSecurityNameCount(map);
        Map layUiMap = new HashMap();
        layUiMap.put("code", 0);
        layUiMap.put("msg", "");
        layUiMap.put("count", count);
        layUiMap.put("data", securities);
        return layUiMap;
    }

    /**
     * 下拉框证券信息，拼接条件的方法
     * @param securityName 证券名称
     * @return
     */
    private String append(String securityName) {
        String sql = "";
        if (securityName != null && !securityName.equals("")) {
            sql += " and securityName=" + securityName;
        }
        return sql;
    }

    /**
     * 分页查询下拉表格的查询证券信息
     * @param securityName 证券名称
     * @return
     */
    public int selectSecurityCount(String securityName) {
        Map map = new HashMap();
        map.put("securityName", securityName);
        int count = securityAccrualStockMapper.selectSecurityIdAndSelectSecurityNameCount(map);
        return count;
    }

}
